package com.cmft.generator.db.service;

import com.cmft.generator.db.entity.TableField;
import com.cmft.generator.db.entity.TableInfo;
import com.cmft.generator.db.tool.DBTool;

import java.sql.ResultSet;
import java.sql.SQLException;

public class OracleTableServiceImpl implements TableService {
    @Override
    public String getTableFieldSql(TableInfo tableInfo) {
        return String.format("SELECT a.COLUMN_NAME ,a.DATA_TYPE ,b.COMMENTS FROM all_tab_columns a,all_col_comments b " +
                "WHERE a.OWNER =b.OWNER " +
                "AND a.TABLE_NAME = b.TABLE_NAME " +
                "AND a.COLUMN_NAME = b.COLUMN_NAME " +
                "AND a.owner='%s' AND a.Table_Name = '%s' ", tableInfo.getTableSchema().toUpperCase(), tableInfo.getTableName().toUpperCase());
    }

    @Override
    public TableField getTableField(ResultSet rs) throws SQLException {
        TableField tableField = new TableField();
        tableField.setColumn(rs.getString("COLUMN_NAME"));
        tableField.setJdbcType(rs.getString("DATA_TYPE"));
        tableField.setComment(rs.getString("COMMENTS"));

        tableField.setProperty(DBTool.tuoFeng(tableField.getColumn()));
        tableField.setJavaType(DBTool.columnTypeToBeanType(tableField.getJdbcType()));
        return tableField;
    }
}
